package com.chat.view;

import com.chat.Local;
import com.chat.service.AddGroupMember;
import com.chat.service.CreatGroup;
import com.chat.service.ManageInviteFriend;
import com.chat.io.ChangeImage;
import com.chat.utils.SnowFlake;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import static com.chat.view.InviteController.a;
import static com.chat.view.MainWindowController.friend;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/*
   创建群聊的Controller类
 */
public class CreateGroupController {
    public TextField groupname;
    public TextField groupmsg;
    public ImageView headpicture;
    public Button select;
    public Label tishi;
    public boolean state=false;//记录有没有选择头像
    public File file;
    public Label nametishi;
    public Label msgtishi;
    public String time;//群聊创建时间
    public ArrayList<String>ff=new ArrayList<>();//存储邀请了哪些好友


    public void select(){//选择头像按钮的点击事件
        FileChooser chooser=new FileChooser();
        chooser.setInitialDirectory(new File("D:\\chatproject\\headimage"));
        chooser.setTitle("选择图片");
        chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PNG","*.png"));
        file=chooser.showOpenDialog(select.getScene().getWindow());
        if(file==null){
            System.out.println("未选择");
        }
        else{
            state=true;
            System.out.println("成功选择");
            System.out.println(file.toURI().toString());
            System.out.println(file.getAbsoluteFile());
            headpicture.setImage(new Image("file:"+file.getAbsolutePath()));
        }
    }
    public void confirm() throws IllegalAccessException {//创建群聊按钮的点击事件
           int flag=0;
           if(groupname.getText().equals(""))
               nametishi.setText("请输入群名！");
           else{
                 flag++;
                 nametishi.setText("");
           }
           if(groupmsg.getText().equals(""))
               msgtishi.setText("请输入群简介！");
           else{
               flag++;
               msgtishi.setText("");
           }
           if(state==false)
               tishi.setText("请选择群头像！");
           else{
                 tishi.setText("");
                 flag++;
           }
            if(flag==3){
                statis();
            }
    }
    public void succeed() throws IllegalAccessException {//群聊创建成功
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        time=dateFormat.format(date);//获取当前时间
         //生成唯一群号
        SnowFlake a=new SnowFlake(1,2);
        long c=a.getNextID();
        String groupid=Long.toString(c%100000000);
        //将头像下载到本地
        ChangeImage ci=new ChangeImage();
        byte[] b=ci.get(file);
        ci.set(groupid,b);
        //向服务端发送建群请求
        CreatGroup cg=new CreatGroup();
        cg.creategroup(groupid,groupname.getText(),groupmsg.getText(),b,time);
        //将群聊加入到好友列表当中
        friend.add(new FriendListData(groupname.getText(),groupid,groupmsg.getText(),"0","1"));
        ///将当前用户加入群成员当中
        AddGroupMember agm=new AddGroupMember();
        for(int i=0;i<ff.size();i++){
          agm.add(groupid,ff.get(i),"1");
        }
        agm.addgroupmember(groupid,Local.user.getUsername(),"3");//将创建群聊的用户设置为群主

        //设置弹窗题型
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setHeaderText("恭喜");
        alert.setContentText("群聊创建成功！群号："+groupid);
        alert.showAndWait();
        //清空输入框
        headpicture.setImage(null);
        groupname.setText("");
        groupmsg.setText("");
    }
    public void statis(){//统计邀请了多少人
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<a.size();i++){
                    InviteFriendController inviteFriendController= ManageInviteFriend.get(a.get(i));
                    if(inviteFriendController.isSelect()){
                        ff.add(a.get(i));
                        System.out.println(a.get(i));
                    }
                }
                try {
                    succeed();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public void invite(){//邀请好友的点击事件
        try {
            InviteView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
